package clientserver;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Quynh
 */

import java.io.*;
import java.net.*;

class TCPServer {
   public static void main(String argv[]) throws Exception
   {
      String clientSentence;
      String capitalizedSentence;
      Integer portno;


      if (argv.length != 1) {
         System.out.println("Usage:  TCPServer <port number>");
	 System.exit(1);
      }


      portno = Integer.parseInt(argv[0]);
      ServerSocket welcomeSocket =
	new ServerSocket(portno);
      System.out.println("Server listening on port " + argv[0] + "..." );
      while(true) {
         Socket connectionSocket = welcomeSocket.accept();
	 BufferedReader inFromClient =
             new BufferedReader(new InputStreamReader(
                     connectionSocket.getInputStream()));

	  PrintStream outToClient =
	     new PrintStream(
		     connectionSocket.getOutputStream());

	 // DataOutputStream outToClient =
         //    new DataOutputStream(
	 //		     connectionSocket.getOutputStream());
         clientSentence = inFromClient.readLine();
	 System.out.println("From Client: " + clientSentence);
	 capitalizedSentence =
	    clientSentence.toUpperCase() + '\n';

	 outToClient.println(capitalizedSentence);
         // outToClient.writeBytes(capitalizedSentence);

      }
    }
}
